﻿module App.Controllers {
    import IStockHistoryService = App.Services.IStockHistoryService;
    import YahooStockQuote = App.Models.IYahooStockQuote;

   

    export interface IStockHistoryController {
        Title: string;
        StartDate: string;
        EndDate: string;
        Symbol: string;
    }

    export class StockHistoryController
        implements IStockHistoryController {
        Title: string;
        StartDate: string;
        EndDate: string;
        Symbol: string;
        StockQuotes: YahooStockQuote[];
        Data: Object[];
        Options: Object;
        HistoryService: IStockHistoryService;
        TransformData(stockQuotes: YahooStockQuote[]) {
            if (!stockQuotes || !stockQuotes.length) return;
            var lows = [], highs = [], adjustedClose = [];
            for (var i = 0; i < stockQuotes.length; i++) {
                var quote = stockQuotes[i];
                lows.push({ x: quote.Date, y: quote.Low });
                highs.push({ x: quote.Date, y: quote.High });
                adjustedClose.push({ x: quote.Date, y: quote.AdjustedClose });
            }
            this.Data = [
                { values: lows.reverse(), key: "Low", color: "#ff7f0e", strokeWidth: 2 },
                { values: highs.reverse(), key: "High", color: "#A1D490", strokeWidth: 2 },
                { values: adjustedClose.reverse(), key: "Adjusted Close", color: "#C390D4", strokeWidth: 2 }
            ];
            return this.Data;
        }
        GetHistory() {
            var ctrl = this;
            this.HistoryService
                .getHistory(ctrl.Symbol, ctrl.StartDate, ctrl.EndDate)
                .then(() => {
                    ctrl.TransformData(ctrl.StockQuotes);
                });
        }

        static $inject = ['StockHistoryService', '$scope'];
        //

        constructor(private service: IStockHistoryService) {
            this.Title = "Stock History";
            this.Symbol = "GOOG";
            this.StockQuotes = service.StockQuotes;
            this.StartDate = "1/1/2016";
            this.EndDate = "12/31/2016";
            // this.log(service);
            this.HistoryService = service;
            this.GetHistory();
            this.Data = this.TransformData(this.StockQuotes);
            this.Options = {
                chart: {
                    type: 'lineChart',
                    height: 450,
                    transitionDuration: 900,
                    margin: {
                        top: 20,
                        right: 30,
                        bottom: 40,
                        left: 70
                    },
                    x(d: any) {
                        if (!d || !d.x) return undefined;
                        if (typeof (d.x) == "number") return d.x;
                        return d.x.getTime();
                    },
                    y(d: any) {
                        if (!d) return;
                        if (!d.y) console.log(d);
                        else
                            return d.y;
                    },
                    useInteractiveGuideline: true,
                    interactive: true,
                    dispatch: {
                        stateChange: e => { console.log("stateChange"); },
                        changeState: e => { console.log("changeState"); },
                        tooltipShow: e => { console.log("tooltipShow"); },
                        tooltipHide: e => { console.log("tooltipHide"); }
                    },
                    xAxis: {
                        axisLabel: "Date",
                        tickFormat(d) {
                            return (new Date(d)).toLocaleDateString();
                        }
                    },
                    yAxis: {
                        axisLabel: "$ USD",
                        tickFormat(d) {
                            return d3.format('.02f')(d);
                        },
                        axisLabelDistance: -10
                    },
                    callback: chart => {
                    }
                },
                title: {
                    enable: false
                }
            };

            //$scope.stockQuotes = [{ Date: new Date("04/12/2016"), Open: 738, High: 743.830017, Low: 731.01001, Close: 743.090027, Volume: 1349700, AdjustedClose: 743.090027 }, { Date: new Date("04/11/2016"), Open: 743.02002, High: 745, Low: 736.049988, Close: 736.099976, Volume: 1211800, AdjustedClose: 736.099976 }, { Date: new Date("04/08/2016"), Open: 743.969971, High: 745.450012, Low: 735.549988, Close: 739.150024, Volume: 1285800, AdjustedClose: 739.150024 }, { Date: new Date("04/07/2016"), Open: 745.369995, High: 747, Low: 736.280029, Close: 740.280029, Volume: 1429500, AdjustedClose: 740.280029 }, { Date: new Date("04/06/2016"), Open: 735.77002, High: 746.23999, Low: 735.559998, Close: 745.690002, Volume: 1050200, AdjustedClose: 745.690002 }, { Date: new Date("04/05/2016"), Open: 738, High: 742.799988, Low: 735.369995, Close: 737.799988, Volume: 1129800, AdjustedClose: 737.799988 }, { Date: new Date("04/04/2016"), Open: 750.059998, High: 752.799988, Low: 742.429993, Close: 745.289978, Volume: 1134200, AdjustedClose: 745.289978 }, { Date: new Date("04/01/2016"), Open: 738.599976, High: 750.340027, Low: 737, Close: 749.909973, Volume: 1574900, AdjustedClose: 749.909973 }, { Date: new Date("03/31/2016"), Open: 749.25, High: 750.849976, Low: 740.940002, Close: 744.950012, Volume: 1712400, AdjustedClose: 744.950012 }, { Date: new Date("03/30/2016"), Open: 750.099976, High: 757.880005, Low: 748.73999, Close: 750.530029, Volume: 1781000, AdjustedClose: 750.530029 }, { Date: new Date("03/29/2016"), Open: 734.590027, High: 747.25, Low: 728.76001, Close: 744.77002, Volume: 1902100, AdjustedClose: 744.77002 }, { Date: new Date("03/28/2016"), Open: 736.789978, High: 738.98999, Low: 732.5, Close: 733.530029, Volume: 1299800, AdjustedClose: 733.530029 }, { Date: new Date("03/24/2016"), Open: 732.01001, High: 737.747009, Low: 731, Close: 735.299988, Volume: 1564800, AdjustedClose: 735.299988 }, { Date: new Date("03/23/2016"), Open: 742.359985, High: 745.719971, Low: 736.150024, Close: 738.059998, Volume: 1421900, AdjustedClose: 738.059998 }, { Date: new Date("03/22/2016"), Open: 737.460022, High: 745, Low: 737.460022, Close: 740.75, Volume: 1264400, AdjustedClose: 740.75 }, { Date: new Date("03/21/2016"), Open: 736.5, High: 742.5, Low: 733.515991, Close: 742.090027, Volume: 1831800, AdjustedClose: 742.090027 }, { Date: new Date("03/18/2016"), Open: 741.859985, High: 742, Low: 731.830017, Close: 737.599976, Volume: 2796400, AdjustedClose: 737.599976 }, { Date: new Date("03/17/2016"), Open: 736.450012, High: 743.070007, Low: 736, Close: 737.780029, Volume: 1856800, AdjustedClose: 737.780029 }, { Date: new Date("03/16/2016"), Open: 726.369995, High: 737.469971, Low: 724.51001, Close: 736.090027, Volume: 1572300, AdjustedClose: 736.090027 }, { Date: new Date("03/15/2016"), Open: 726.919983, High: 732.289978, Low: 724.77002, Close: 728.330017, Volume: 1720100, AdjustedClose: 728.330017 }, { Date: new Date("03/14/2016"), Open: 726.809998, High: 735.5, Low: 725.150024, Close: 730.48999, Volume: 1716900, AdjustedClose: 730.48999 }, { Date: new Date("03/11/2016"), Open: 720, High: 726.919983, Low: 717.125, Close: 726.820007, Volume: 1963900, AdjustedClose: 726.820007 }, { Date: new Date("03/10/2016"), Open: 708.119995, High: 716.440002, Low: 703.359985, Close: 712.820007, Volume: 2829400, AdjustedClose: 712.820007 }, { Date: new Date("03/09/2016"), Open: 698.469971, High: 705.679993, Low: 694, Close: 705.23999, Volume: 1418700, AdjustedClose: 705.23999 }, { Date: new Date("03/08/2016"), Open: 688.590027, High: 703.789978, Low: 685.340027, Close: 693.969971, Volume: 2063400, AdjustedClose: 693.969971 }, { Date: new Date("03/07/2016"), Open: 706.900024, High: 708.091003, Low: 686.900024, Close: 695.159973, Volume: 2985100, AdjustedClose: 695.159973 }, { Date: new Date("03/04/2016"), Open: 714.98999, High: 716.48999, Low: 706.02002, Close: 710.890015, Volume: 1967900, AdjustedClose: 710.890015 }, { Date: new Date("03/03/2016"), Open: 718.679993, High: 719.450012, Low: 706.02002, Close: 712.419983, Volume: 1956800, AdjustedClose: 712.419983 }, { Date: new Date("03/02/2016"), Open: 719, High: 720, Low: 712, Close: 718.849976, Volume: 1627800, AdjustedClose: 718.849976 }, { Date: new Date("03/01/2016"), Open: 703.619995, High: 718.809998, Low: 699.77002, Close: 718.809998, Volume: 2147400, AdjustedClose: 718.809998 }, { Date: new Date("02/29/2016"), Open: 700.320007, High: 710.890015, Low: 697.679993, Close: 697.77002, Volume: 2280300, AdjustedClose: 697.77002 }, { Date: new Date("02/26/2016"), Open: 708.580017, High: 713.429993, Low: 700.859985, Close: 705.070007, Volume: 2240000, AdjustedClose: 705.070007 }, { Date: new Date("02/25/2016"), Open: 700.01001, High: 705.97998, Low: 690.585022, Close: 705.75, Volume: 1631900, AdjustedClose: 705.75 }, { Date: new Date("02/24/2016"), Open: 688.919983, High: 700, Low: 680.780029, Close: 699.559998, Volume: 1958600, AdjustedClose: 699.559998 }, { Date: new Date("02/23/2016"), Open: 701.450012, High: 708.400024, Low: 693.580017, Close: 695.849976, Volume: 1999700, AdjustedClose: 695.849976 }, { Date: new Date("02/22/2016"), Open: 707.450012, High: 713.23999, Low: 702.51001, Close: 706.460022, Volume: 1946100, AdjustedClose: 706.460022 }, { Date: new Date("02/19/2016"), Open: 695.030029, High: 703.080994, Low: 694.049988, Close: 700.909973, Volume: 1582300, AdjustedClose: 700.909973 }, { Date: new Date("02/18/2016"), Open: 710, High: 712.349976, Low: 696.030029, Close: 697.349976, Volume: 1859100, AdjustedClose: 697.349976 }, { Date: new Date("02/17/2016"), Open: 698.090027, High: 709.75, Low: 691.380005, Close: 708.400024, Volume: 2490000, AdjustedClose: 708.400024 }, { Date: new Date("02/16/2016"), Open: 692.97998, High: 698, Low: 685.049988, Close: 691, Volume: 2520000, AdjustedClose: 691 }, { Date: new Date("02/12/2016"), Open: 690.26001, High: 693.75, Low: 678.599976, Close: 682.400024, Volume: 2141400, AdjustedClose: 682.400024 }, { Date: new Date("02/11/2016"), Open: 675, High: 689.349976, Low: 668.867981, Close: 683.109985, Volume: 3024000, AdjustedClose: 683.109985 }, { Date: new Date("02/10/2016"), Open: 686.859985, High: 701.309998, Low: 682.130005, Close: 684.119995, Volume: 2638000, AdjustedClose: 684.119995 }, { Date: new Date("02/09/2016"), Open: 672.320007, High: 699.900024, Low: 668.77002, Close: 678.109985, Volume: 3608900, AdjustedClose: 678.109985 }, { Date: new Date("02/08/2016"), Open: 667.849976, High: 684.030029, Low: 663.059998, Close: 682.73999, Volume: 4247400, AdjustedClose: 682.73999 }, { Date: new Date("02/05/2016"), Open: 703.869995, High: 703.98999, Low: 680.150024, Close: 683.570007, Volume: 5105700, AdjustedClose: 683.570007 }, { Date: new Date("02/04/2016"), Open: 722.809998, High: 727, Low: 701.859985, Close: 708.01001, Volume: 5168700, AdjustedClose: 708.01001 }, { Date: new Date("02/03/2016"), Open: 770.219971, High: 774.5, Low: 720.5, Close: 726.950012, Volume: 6171000, AdjustedClose: 726.950012 }, { Date: new Date("02/02/2016"), Open: 784.5, High: 789.869995, Low: 764.650024, Close: 764.650024, Volume: 6348100, AdjustedClose: 764.650024 }, { Date: new Date("02/01/2016"), Open: 750.460022, High: 757.859985, Low: 743.27002, Close: 752, Volume: 5139200, AdjustedClose: 752 }];
        }
    }

    angular
        .module("app")
        .controller("StockHistoryController", StockHistoryController);
}